import { useMessage } from '@hooks/useAppMessage'
import { useAxios } from '@hooks/useAxios'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { API_CONFIGS_URL } from './baseApi'

export const useConfigsQuery = (params) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['configs', params],
    queryFn: async () => await http.get(API_CONFIGS_URL, { params }),
  })
}

export const useConfigsUpdateMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.put(`${API_CONFIGS_URL}`, data),
    onSuccess: (data) => {
      console.log('%c Line:22 🥔 data', 'color:#ed9ec7', data)
      queryClient.invalidateQueries(['configs'])
      message.success('更新成功')
    },
  })
}
